package com.tju.resource.business.tb_outward.report;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tju.resource.business.tb_outward.TbOutwardService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.result.SimpleXls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class TbOWReportServiceImpl implements TbOWReportService {

    private static final String baseXlsPath = TbOWReportServiceImpl.class.getResource("/res/xlsTemplate/TbOutWard").toString();

    @Autowired
    TbOutwardService tbOutwardService;

    @Override
    public boolean export(JSONObject data, OutputStream out) {
        String file = baseXlsPath + "/HY_Out_Ward.xls";
        InputStream inputStream = TbOWReportServiceImpl.class.getResourceAsStream("/res/xlsTemplate/TbOutWard/HY_Out_Ward.xls");
        boolean exist = FileUtil.exist(file);

        if (!exist && !file.contains("jar:")) {
            return false;
        } else {
            try {
                Map<String, String> cParams = new HashMap<>();
                Result<Object> objectResult;
                JSONArray list;
                JSONArray dataList = new JSONArray();
                JSONObject onlyData;
                AtomicInteger i = new AtomicInteger(1);

                cParams.put("owId", data.getStr("owId"));
//                cParams.put("state", data.getStr("state"));
                cParams.put("page", "1");
                cParams.put("limit", "99999");

                objectResult = tbOutwardService.getAll(cParams);
                list = JSONUtil.parseArray(objectResult.getData());
                onlyData = JSONUtil.parseObj(list.get(0));

                onlyData.getJSONArray("outwardOrderList").forEach(v -> {
                    JSONObject fData = new JSONObject(JSONUtil.parseObj(v));
                    JSONObject material = fData.getJSONObject("tbMaterial");

                    fData.putOnce("matlName", material.getStr("matlName"));
                    fData.putOnce("color", material.getStr("color"));
                    fData.putOnce("pcsUnit", material.getStr("pcsUnit"));
                    fData.putOnce("i", i.get());

                    i.incrementAndGet();
                    dataList.add(fData);
                });
                data.putAll(onlyData);
                data.putAll(onlyData.getJSONObject("tbSupplier"));
                data.set("outgoingTime", DateUtil.formatDate(data.getDate("outgoingTime")));
                data.set("deliveryTime", DateUtil.formatDate(data.getDate("deliveryTime")));

                data.remove("list");
                data.putOnce("list", dataList);

                if (file.indexOf("jar:") == 0) {
                    SimpleXls.fillTemplate(file, inputStream, data, out);
                } else {
                    SimpleXls.fillTemplate(FileUtil.file(file).getAbsolutePath(), data, out);
                }

                return true;
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    out.write(ResultUtil.error(ResultEnum.UNKONW_ERROR, "could down excel file, cause: " + e.toString()).toString().getBytes());
                } catch (Exception e1) {
                    return false;
                }
                return false;
            }
        }
    }
}
